function withData(param) {
	return param < 10 ? '0' + param : '' + param;
}

function getLoopArray(start, end) {
	var start = start || 0;
	var end = end || 1;
	var array = [];
	for (var i = start; i <= end; i++) {
		array.push(withData(i));
	}
	return array;
}

function getMonthDay(year, month) {
	var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
		array = null;

	switch (month) {
		case '01':
		case '03':
		case '05':
		case '07':
		case '08':
		case '10':
		case '12':
			array = getLoopArray(1, 31)
			break;
		case '04':
		case '06':
		case '09':
		case '11':
			array = getLoopArray(1, 30)
			break;
		case '02':
			array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
			break;
		default:
			array = '月份格式不正确，请重新输入！'
	}
	return array;
}

function getNewDateArry() {
	// 当前时间的处理 年月日时分秒
	var newDate = new Date();
	var year = withData(newDate.getFullYear()),
		mont = withData(newDate.getMonth() + 1),
		date = withData(newDate.getDate()),
		hour = withData(newDate.getHours()),
		minu = withData(newDate.getMinutes());
	// seco = withData(newDate.getSeconds());

	return [year, mont, date, hour, minu];
}

function dateTimePicker(startYear, endYear, date) {
	// 返回默认显示的数组和联动数组的声明
	var dateTime = [],
		dateTimeArray = [
			[],
			[],
			[],
			[],
			[]
		];
	var start = startYear || 1978;
	var end = endYear || 2100;

	//处理传过来的字符串转化为数组
	let dataArr = date.split(" ")[0].split('/')
	let time = date.split(" ")[1].split(':')
	// 默认开始显示数据 如果把自定义值传过来的话就使用自定义时间 否则用当前时间  ...dataArr数组解构
	var defaultDate = date ? [...dataArr, ...time] : getNewDateArry();

	// 处理联动列表数据
	/*年月日 时分秒*/
	dateTimeArray[0] = getLoopArray(start, end);
	dateTimeArray[1] = getLoopArray(1, 12);
	dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
	dateTimeArray[3] = getLoopArray(0, 23);
	dateTimeArray[4] = getLoopArray(0, 59);
	// dateTimeArray[5] = getLoopArray(0, 59);

	//遍历dateTimeArray数组
	dateTimeArray.forEach((current, index) =>
		//匹配defaultDate[index]值在current数组中的位置
		dateTime.push(current.indexOf(defaultDate[index]))
	);

	return {
		dateTimeArray: dateTimeArray,
		dateTime: dateTime
	}
}

function getDateDiff(dateTimeStamp) {
	// #ifdef APP
	if (plus.os.name == 'iOS') {
		dateTimeStamp = dateTimeStamp.replaceAll("-", "/")
	}
	// #endif
	if (typeof dateTimeStamp == 'string') {
		dateTimeStamp = dateTimeStamp.replace(/-/g, "/")
	}
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var dateTime = new Date(dateTimeStamp).getTime()
	var diffValue = now - dateTime;
	if (diffValue < 0) {
		return;
	}
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	var result = '';
	if (monthC >= 1) { //x月x日
		result = gettimeZY(dateTimeStamp)
	} else if (weekC >= 1) { //x月x日
		result = gettimeZY(dateTimeStamp)
	} else if (parseInt(dayC) == 1) {
		result = "昨天";
	} else if (parseInt(dayC) > 1) { //x月x日
		result = gettimeZY(dateTimeStamp, 1)
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前";
	} else
		result = "刚刚";
	return result;
}
//转译
function gettimeZY(date) {
	if (typeof date == 'string') {
		date = date.replace(/-/g, "/")
	}
	var newDate = new Date(date);
	var year = newDate.getFullYear();
	var mont = withData(newDate.getMonth() + 1);
	var date = withData(newDate.getDate());
	var hour = withData(newDate.getHours());
	var minute = withData(newDate.getMinutes());
	var second = withData(newDate.getSeconds());
	var result = ''
	var nDate = new Date();
	var year2 = nDate.getFullYear();

	if (year2 > year) {
		result = year + '年' + mont + '月' + date + '日'
	} else {
		result = mont + '月' + date + '日'
	}
	return result;
}
//转译
function getTimeZY(date) {
	if (typeof date == 'string') {
		date = date.replace(/-/g, "/")
	}
	var newDate = new Date(date);
	var year = newDate.getFullYear();
	var mont = withData(newDate.getMonth() + 1);
	var date = withData(newDate.getDate());
	var hour = withData(newDate.getHours());
	var minute = withData(newDate.getMinutes());
	var second = withData(newDate.getSeconds());
	var result = ''
	var nDate = new Date();
	var year2 = nDate.getFullYear();

	result = year + '年' + mont + '月' + date + '日'
	return result;
}
//转译
function getTimeDayZY(date) {
	if (typeof date == 'string') {
		date = date.replace(/-/g, "/")
	}
	var newDate = new Date(date);
	var year = newDate.getFullYear();
	var mont = withData(newDate.getMonth() + 1);
	var date = withData(newDate.getDate());
	var hour = withData(newDate.getHours());
	var minute = withData(newDate.getMinutes());
	var second = withData(newDate.getSeconds());
	var result = ''
	var nDate = new Date();
	var year2 = nDate.getFullYear();

	result = year + '年' + mont + '月' + date + '日 ' + hour + ':' + minute + ':' + second
	return result;
}

// 2024-01 转为 2024年1月
function formatDateAdjusted(dateStr) {
	const [year, month] = dateStr.split('-');
	return `${year}年${month}月`;
}

// 获取当前时间
function getTimes() {
	var newDate = new Date();
	var year = newDate.getFullYear();
	var mont = withData(newDate.getMonth() + 1);
	var date = withData(newDate.getDate());
	var hour = withData(newDate.getHours());
	var minute = withData(newDate.getMinutes());
	var second = withData(newDate.getSeconds());
	var result = year + '-' + mont + '-' + date + ' ' + hour + ':' + minute + ':' + second;
	return result;
}
//获取当前年份
function getYear(string) {
	let year = null
	if (string) {
		year = String(string).slice(0, 4)
	} else {
		const newDate = new Date();
		year = newDate.getFullYear();
	}
	return year;
}




function formatTime2(fmt, timestamp) {
	var date = new Date(timestamp); // 兼容safari
	var o = {
		"M+": date.getMonth() + 1,
		"d+": date.getDate(),
		"h+": date.getHours(),
		"m+": date.getMinutes(),
		"s+": date.getSeconds(),
		"q+": Math.floor((date.getMonth() + 3) / 3),
		S: date.getMilliseconds()
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(
			RegExp.$1,
			(date.getFullYear() + "").substr(4 - RegExp.$1.length)
		);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
			);
		}
	}
	return fmt;
}

//根据生日计算年龄
function getAge(birthDateString) {
	// 将出生时间字符串解析为 Date 对象
	const birthDate = new Date(birthDateString);

	// 获取今天的日期
	const today = new Date();

	// 获取年份、月份和日期的差值
	let age = today.getFullYear() - birthDate.getFullYear();
	const monthDiff = today.getMonth() - birthDate.getMonth();
	const dayDiff = today.getDate() - birthDate.getDate();

	// 如果今天的月份小于出生月份，或者月份相同但今天的日期小于出生日期，则年龄减一
	if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
		age--;
	}

	return age;
}


module.exports = {
	dateTimePicker: dateTimePicker,
	getMonthDay: getMonthDay,
	getDateDiff: getDateDiff,
	getTimeZY: getTimeZY,
	getTimeDayZY: getTimeDayZY,
	getTimes: getTimes,
	formatTime2: formatTime2,
	getYear: getYear,
	getAge: getAge,
	formatDateAdjusted: formatDateAdjusted
}